<?php
////////////////////////////////////////////////////
//	LinBoard
//	Framework/Post.php
//	Created: 19th February 2009; 13:48 UTC
//	LinBoard (c) Copyright OpenInferno 2008, 2009
////////////////////////////////////////////////////

//Setup Environment
error_reporting(E_ALL | E_STRICT);

//Direct Access Check
if(!defined("LINBOARD"))
{
	die("<h1>Access Error</h1>Direct Access to this script is forbidden.");
}

class Post
{
	public function getPosts($Threadid, $Startfrom = 0, $Limit = 10)
	{
		Framework::Mysql()->requireConnection();
		
		//Filter data
		if(isset($Threadid) && is_numeric($Threadid) && isset($Startfrom) && is_numeric($Startfrom) && isset($Limit) && is_numeric($Limit))
		{
			$Threadid = (int)$Threadid;
			$Startfrom = (int)$Startfrom;
			$Limit = (int)$Limit;
		} else {
			return false;
		}
		
		//Perform Query
		$result = Framework::Mysql()->query("SELECT p.*, u.*, g.*
											FROM `".Framework::Config()->Mysql['prefix']."posts` p
											LEFT JOIN `".Framework::Config()->Mysql['prefix']."users` u ON (u.uid = p.posterid)
											LEFT JOIN `".Framework::Config()->Mysql['prefix']."groups` g ON (g.gid = u.usergroup)
											WHERE `parent` = {$Threadid}
											ORDER BY `postime` ASC LIMIT {$Startfrom}, {$Limit};");
		if(Framework::Mysql()->numRows($result) < 1)
		{
			return false;
		}
		$Posts = array();
		while($row = Framework::Mysql()->fetchArray($result))
		{
			$Posts['id'][] = $row['id'];
			$Posts['posterid'][] = $row['posterid'];
			$Posts['title'][] = htmlentities(stripslashes($row['title']));
			$Posts['text'][] = Framework::Misc()->stylePost(nl2br(htmlentities(stripslashes($row['text']))));
			$Posts['postime'][] = $row['postime'];
			$Posts['parent'][] = $row['parent'];
			$Posts['edithistory'][] = $row['edithistory'];
			$Posts['uid'][] = $row['uid'];
			$Posts['username'][] = stripslashes($row['username']);
			$Posts['cleanusername'][] = stripslashes($row['username']);
			$Posts['totalposts'][] = $row['totalposts'];
			$Posts['location'][] = htmlentities(stripslashes($row['location']));
			$Posts['email'][] = htmlentities(stripslashes($row['email']));
			$Posts['gender'][] = $row['gender'];
			$Posts['usertitle'][] = htmlentities(stripslashes($row['usertitle']));
			$Posts['IM'][] = stripslashes($row['IM']);
			$Posts['signature'][] = nl2br(htmlentities(stripslashes($row['signature'])));
			$Posts['gid'][] = $row['gid'];
			$Posts['gname'][] = $row['gname'];
			$Posts['utitle'][] = $row['utitle'];
			$Posts['global_permissions'][] = $row['global_permissions'];
		}
		
		return $Posts;
	}
	
	public function writePost($pid, $title, $text, $parent)
	{
		Framework::Mysql()->requireConnection();
		
		//Filter Data
		if(isset($pid) && is_numeric($pid) && isset($title) && isset($text) && isset($parent) && is_numeric($parent))
		{
			$pid = (int)$pid;
			$title = Framework::Mysql()->escapeString($title);
			$text = Framework::Mysql()->escapeString($text);
			$parent = (int)$parent;
		} else {
			return false;
		}
		
		Framework::Mysql()->query("INSERT INTO `".Framework::Config()->Mysql['prefix']."posts` (`posterid`, `title`, `text`, `postime`, `parent`, `edithistory`) VALUES ('{$pid}', '{$title}', '{$text}', '".time()."', '{$parent}', '');");
		
		//Update User Post Count
		Framework::Mysql()->query("UPDATE `".Framework::Config()->Mysql['prefix']."users` SET `totalposts` = `totalposts` + 1 WHERE `uid` = '{$pid}' LIMIT 1;");
		
		//Update Thread Post Count
		Framework::Mysql()->query("UPDATE `".Framework::Config()->Mysql['prefix']."threads` SET `posts` = `posts` + 1, `latestpostime` = '".time()."' WHERE `id` = '{$parent}' LIMIT 1;");
		
		return true;
	}
	
	public static function getInstance()
	{
		static $instance;
		if(!isset($instance))
		{
			$class = __CLASS__;
			$instance = new $class;
		}
		return $instance;
	}
	
}

?>